package com.nine.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nine.entity.Package;
import com.nine.entity.Packageitems;
import com.nine.service.PackageService;

@Controller
public class PackageController {
	@Autowired
	private PackageService packageService;
	
	/**
	 * 异步获取所有套餐信息（支持分页）
	 * @param limit 分页大小
	 * @param page 当前页码
	 * @return 返回数据
	 */
	@ResponseBody
	@RequestMapping("showpackage.do")
	public Map<String,Object> getAllPackage(Integer limit, Integer page) {
		//利用mybatis时的分页功能
		//limit表示页大小，page表示页码
		//初始判断page为空则指定页码为1
		//page=page==null?1:page;
		
		//1.启用分页
		PageHelper.startPage(page, limit); //传页码，页大小
		
		//2.查询
		//执行查询修改sql，自动添加limit进行分页
		List<Package> list = packageService.getAllPackage();
				
		//3.获取分页后的相关信息
		PageInfo<Package> pageInfo=new PageInfo<Package>(list);
		Integer count = (int) pageInfo.getTotal();
		
		//layui对返回数据的json格式要求
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code", 0);
		resultMap.put("msg", "");
		resultMap.put("count", count);
		resultMap.put("data", list);
		return resultMap;
	}
	
	/**
	 * 条件查询套餐信息（支持分页和模糊查询）
	 * @param limit 分页大小
	 * @param page 当前页码
	 * @param pgid 套餐编号
	 * @param pgname 套餐名称
	 * @return 返回数据
	 */
	@ResponseBody
	@RequestMapping("getpackagebykey.do")
	public Map<String,Object> getAllPackageByCondition(Integer limit, Integer page,String pgid,String pgname) {
		//1.启用分页
		PageHelper.startPage(page, limit); //传页码，页大小
		
		//2.查询
		//执行查询修改sql，自动添加limit进行分页
		Package p = new Package();
		p.setPgid(pgid);
		p.setPgname(pgname);
		List<Package> list = packageService.getAllPackageByCondition(p);
				
		//3.获取分页后的相关信息
		PageInfo<Package> pageInfo=new PageInfo<Package>(list);
		Integer count = (int) pageInfo.getTotal();
		
		//layui对返回数据的json格式要求
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code", 0);
		resultMap.put("msg", "");
		resultMap.put("count", count);
		resultMap.put("data", list);
		return resultMap;
	}
	
	/**
	 * 根据套餐编号查询套餐信息
	 * @param pgid 套餐编号
	 * @return 返回操作状态码和商品数据
	 */
	@ResponseBody
	@RequestMapping("getpackagebypgid.do")
	public Map<String, Object> getPackageByPgid(String pgid) {
		Package p = packageService.getPackage(pgid);
		Integer code = p==null?0:1;
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code", code);
		resultMap.put("data", p);
		return resultMap;
	}
	
	/**
	 * 添加套餐
	 * @param p 套餐实体
	 * @return 返回操作状态码
	 */
	@ResponseBody
	@RequestMapping("addpackage.do")
	public Map<String, Object> addPackage(@RequestBody Package p) {
		Integer code = -1;
		code = packageService.addPackage(p);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		if(code>=1) {
			resultMap.put("code", 1);
			resultMap.put("msg", "操作成功");
		}else {
			resultMap.put("code", code);
			resultMap.put("msg", "操作失败");
		}
		return resultMap;
	}
	
	/**
	 * 修改套餐信息
	 * @param p 套餐实体
	 * @return 返回操作状态码
	 */
	@ResponseBody
	@RequestMapping("updatepackage.do")
	public Map<String, Object> updatePackage(@RequestBody Package p) {
		Integer code = -1;
		code = packageService.updatePackage(p);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		if(code>=1) {
			resultMap.put("code", 1);
			resultMap.put("msg", "操作成功");
		}else {
			resultMap.put("code", code);
			resultMap.put("msg", "操作失败");
		}
		return resultMap;
	}
	
	/**
	 * 删除套餐
	 * @param pgid 套餐编号
	 * @return 返回操作状态码
	 */
	@ResponseBody
	@RequestMapping("deletepackage.do")
	public Map<String, Object> deletePackage(String pgid) {
		Integer code = -1; 
		code = packageService.deletePackage(pgid);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		if(code>=1) {
			resultMap.put("code", 1);
			resultMap.put("msg", "操作成功");
		}else {
			resultMap.put("code", code);
			resultMap.put("msg", "操作失败");
		}
		return resultMap;
	}
	
	/**
	 * 删除套餐
	 * 支持单条和多条操作
	 * @param pgids 套餐编号(用,隔开)
	 * @return 返回操作状态码
	 */
	@ResponseBody
	@RequestMapping("deletemorepackage.do")
	public Map<String, Object> deleteMorePackage(String pgids) {
		String[] pgid = pgids.split(",");
		
		Integer code = -1; 
		code=packageService.deleteMorePackage(pgid);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		if(code>=1) {
			resultMap.put("code", 1);
			resultMap.put("msg", "操作成功");
		}else {
			resultMap.put("code", code);
			resultMap.put("msg", "操作失败");
		}
		return resultMap;
	}
	
	/**
	 * 添加套餐明细
	 * @param pgid 套餐编号
	 * @param m 商品键值对数组
	 * @param list 商品实体集合
	 * @return 返回数据
	 */
	@ResponseBody
	@RequestMapping("addpackageitems.do")
	public Map<String, Object> addPackageitems(String pgid, @RequestBody HashMap<String, Object>[] m) {
		Integer code = -1;
		Packageitems items = null;
		List<Packageitems> list = new ArrayList<Packageitems>();
		for (HashMap<String, Object> hm : m) {
			items = new Packageitems();
			items.setPackageid(pgid);
			items.setProductid((String) hm.get("pid"));
			items.setNumber(new Integer((String) hm.get("count")));
			list.add(items);
		}
		code = packageService.addPackageitems(list);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		if(code>=1) {
			resultMap.put("code", 1);
			resultMap.put("msg", "操作成功");
		}else {
			resultMap.put("code", code);
			resultMap.put("msg", "操作失败");
		}
		return resultMap;
	}
	
	/**
	 * 通过套餐编号查看套餐明细
	 * @param limit 分页大小
	 * @param page 当前页码
	 * @param pgid 套餐编号
	 * @return 返回数据
	 */
	@ResponseBody
	@RequestMapping("getpackageitemsbypgid.do")
	public Map<String,Object> getPackageitemsByPgid(Integer limit, Integer page,String pgid){
		//1.启用分页
		PageHelper.startPage(page, limit); //传页码，页大小
		
		//2.查询
		//执行查询修改sql，自动添加limit进行分页
		List<Packageitems> list = packageService.getPackageitemsByPgid(pgid);
		
		//3.获取分页后的相关信息
		PageInfo<Packageitems> pageInfo=new PageInfo<Packageitems>(list);
		Integer count = (int) pageInfo.getTotal();
		
		//layui对返回数据的json格式要求
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code", 0);
		resultMap.put("msg", "");
		resultMap.put("count", count);
		resultMap.put("data", list);
		return resultMap;
	}
	
	/**
	 * 通过套餐明细编号删除套餐明细记录
	 * @param id 套餐明细编号
	 * @return 返回数据
	 */
	@ResponseBody
	@RequestMapping("deletepackageitemsbyid.do")
	public Map<String, Object> deletePackageitemsById(Integer id) {
		Integer code = -1; 
		code = packageService.deletePackageitemsById(id);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		if(code>=1) {
			resultMap.put("code", 1);
			resultMap.put("msg", "操作成功");
		}else {
			resultMap.put("code", code);
			resultMap.put("msg", "操作失败");
		}
		return resultMap;
	}
	
}
